#include "McuWorker.h"
#include "BootSetting.h"
#include "ImageSetting.h"
#include "MiscData.h"
#include "HintDialog.h"
#include "Mcu.h"
#include "Setting.h"
#include "ScreenshotWorker.h"
#include "SensorInfo.h"
#include "WidgetHandler.h"
McuWorker *McuWorker::getInstance()
{
    static McuWorker singleton{};
    return &singleton;
}

/* value: 1 - 8 ---> 面板按键key1 - key8 */
void McuWorker::reportKeyValueSlot(uint8_t value)
{
    int mirrorType = 0;
    int itemValue = 0;
    uint32_t itemValue32bit = 0;
    int screenshot = -1;
    uint32_t type = SensorInfo::getInstance().getSensorType();
    qDebug()<<"key value = "<<value;
    BaseWidget *home =  WidgetHandler::getInstance()->getWidget(HomeUi_Id);
    switch (value) {
    case 1:/* 亮度- */
        /* 设置fpga 更新ui */
        mirrorType = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentMirrorType;
        itemValue = RuntimeData::ImageSetting::getInstance()->g_settingItemValue[mirrorType][ImageBrightness_Index];
        itemValue--;
        checkSettingValue(ImageBrightness_Index, &itemValue);
        RuntimeData::ImageSetting::getInstance()->g_settingItemValue[mirrorType][ImageBrightness_Index] = itemValue;
        RuntimeData::ImageSetting::getInstance()->saveImageSettingToRom(mirrorType);
        setFpga[ImageBrightness_Index](itemValue, type);
        emit updateImageSettingItemUiSignal();
        emit updateToastSignal(value, itemValue);
        break;

    case 2:/* 亮度+ */
        mirrorType = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentMirrorType;
        itemValue = RuntimeData::ImageSetting::getInstance()->g_settingItemValue[mirrorType][ImageBrightness_Index];
        itemValue++;
        checkSettingValue(ImageBrightness_Index, &itemValue);
        RuntimeData::ImageSetting::getInstance()->g_settingItemValue[mirrorType][ImageBrightness_Index] = itemValue;
        RuntimeData::ImageSetting::getInstance()->saveImageSettingToRom(mirrorType);
        setFpga[ImageBrightness_Index](itemValue, type);
        emit updateImageSettingItemUiSignal();
        emit updateToastSignal(value, itemValue);
        break;
    case 11:
    case 12:
    case 3:/* 录像 */
        screenshot =  RuntimeData::MiscData::getInstance()->g_miscDataInfo.screenshot;
        if (screenshot >= 0) {
            ScreenshotWorker::getInstance()->screenshot();
            return;
        }
        emit recordSignal();
        break;
    case 14:
    case 4:/* 冻结 */
        itemValue = RuntimeData::MiscData::getInstance()->g_miscDataInfo.imageFreezeState;
        itemValue = itemValue == 0 ? 1 : 0;
        RuntimeData::MiscData::getInstance()->g_miscDataInfo.imageFreezeState = itemValue;
        setFpga[ImageFreeze_Index](itemValue, type);
        emit updateToastSignal(4, itemValue);
        break;

    case 5:/* zoom */
        emit updateToastSignal(value, itemValue);
        break;

    case 6:/* 白平衡 */
        itemValue32bit = 1024;/* r */
        itemValue32bit = (itemValue32bit << 16) | 1024;
        setFpga[ImageAwb_Index](itemValue32bit, type);
        emit updateToastSignal(value, itemValue32bit);
        break;

    case 7:/* 镜子类型 */
        if(home&&!home->isHidden()) {
            itemValue = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentMirrorType;
            itemValue++;
            if (itemValue > 5)
                itemValue = 0;

            emit updateMirrorTypeSignal(itemValue);
            emit updateToastSignal(value, itemValue);
        }
        break;

    case 8:/* 复位 */
        emit updateToastSignal(value);
        break;
    case 13:
    case 9:/* 保存照片 */
        screenshot =  RuntimeData::MiscData::getInstance()->g_miscDataInfo.screenshot;
        if (screenshot >= 0) {
            ScreenshotWorker::getInstance()->exportScreenshot();
            HintDialog::getInstance()->setInfo(tr("截图导出成功!"), HintDialogType_Hint);
            HintDialog::getInstance()->exec();
            return;
        }
        emit takePhotoSignal();
        break;

    case 10:/* 电源开关 */
        HintDialog::getInstance()->setInfo(tr("是否关机!"), McuWorker_SystemShutdown);
        HintDialog::getInstance()->exec();
        break;
    default:
        HintDialog::getInstance()->setInfo(QString("value:%1").arg(value), HintDialogType_Null);
        HintDialog::getInstance()->exec();
    }
}

void McuWorker::systemShutdownSlot()
{
    Mcu::getInstance()->systemShutdownMessage();
}

McuWorker::McuWorker()
{
    QObject::connect(HintDialog::getInstance(), &HintDialog::McuWorker_SystemShutdown_Signal,
                     this, &McuWorker::systemShutdownSlot);
}
